// DlgSetAccount.cpp : 实现文件
//
#define NOMINMAX
#include "AIPanelDataCDC.h"

#include <uam/UserAccountManager.h>

#include <QColor>
#include <QFontMetrics>
#include <QPainter>
#include <QStylePainter>

#include "../StringResource.h"

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// class CAIPanelDataCDC
CAIPanelDataCDC::CAIPanelDataCDC() {
  m_strType = "CDC";
  m_imgSuccess = ImgMgr::instance().image("guiframe_icon_tips_success");
}

CAIPanelDataCDC::~CAIPanelDataCDC() {}

void CAIPanelDataCDC::Init() {
  auto& auth = SSUserAccountManager::instance().GetCDCAutority();
  int line = std::max((int)auth.size(), 1);
  m_nHeight = 24 * (line + 1) + 8 * 2;
}

void CAIPanelDataCDC::Draw(QPainter* painter, int top) {
  auto& auth = SSUserAccountManager::instance().GetCDCAutority();
  bool bValid = auth.size() > 0;
  QColor brHead = QColor(230, 179, 66);
  QColor brFront = NAME_COLOR("TextNormal");

  QString strTemp = NewStringTable::strAIPanelDataCDC(0);  // 数据权限 - 中债
  int nGap = 8;

  // 数据权限-CFETS
  QRect r = rect();
  int X = nGap;
  int Y = 0;

  QRect textRect = QRect(X, Y, r.right() - X, Y + 24);
  painter->setPen(QPen(brHead));
  painter->setFont(QFont(m_lfHead));
  painter->drawText(textRect, strTemp,
                    QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

  X += 24;
  Y += 28;
  int nMaxWidth = 1000;
  int nImgHeight = 20;
  if (m_imgSuccess) nImgHeight = m_imgSuccess->height();

  painter->setFont(QFont(m_lfContent));
  if (bValid) {
    if (m_imgSuccess) {
      int x = X + (24 - nImgHeight) / 2;
      int y = Y + (24 - nImgHeight) / 2;
      QPixmap m_tipssuccess = QPixmap::fromImage(*m_imgSuccess);
      painter->drawPixmap(x, y, m_tipssuccess);
      X += 24 + 1;
    }
    strTemp = NewStringTable::strAIPanelDataCDC(1);  // 您已获得权限:

    QFontMetrics fontMetrics(m_lfContent);
    int nFontWidth = fontMetrics.width(strTemp);

    textRect = QRect(X, Y, nMaxWidth, 24);
    painter->setPen(QPen(brFront));
    painter->drawText(textRect, strTemp,
                      QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

    X += nFontWidth + 5;
    for (auto it = auth.begin(); it != auth.end(); it++) {
      strTemp = QString::fromLocal8Bit(it->second.m_AuthName);

      textRect = QRect(X, Y, nMaxWidth, 24);
      painter->setPen(QPen(brFront));
      painter->drawText(textRect, strTemp,
                        QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

      Y += 24;
    }
  } else {
    // 无权限
    QFontMetrics fontMetrics(m_lfContent);
    int nFontWidth = fontMetrics.width(strTemp);

    strTemp = NewStringTable::strAIPanelDataCDC(2);  // 您暂未开通中债数据权限

    textRect = QRect(X, Y, nMaxWidth, 24);
    painter->setPen(QPen(brFront));
    painter->drawText(textRect, strTemp,
                      QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

    X += nFontWidth;
    strTemp = NewStringTable::strAIPanelDataCDC(3);  // NOT

    fontMetrics = QFontMetrics(m_lfContent);
    nFontWidth = fontMetrics.width(strTemp);

    textRect = QRect(X, Y, nMaxWidth, 24);
    painter->setPen(QPen(brHead));
    painter->drawText(textRect, strTemp,
                      QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

    X += nFontWidth;
    strTemp = NewStringTable::strAIPanelDataCDC(
        4);  // been authorized to access CDC data.
    textRect = QRect(X, Y, nMaxWidth, 24);
    painter->setPen(QPen(brFront));
    painter->drawText(textRect, strTemp,
                      QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
  }
}

void CAIPanelDataCDC::paintEvent(QPaintEvent* event) {
  QStylePainter painter(this);
  Draw(&painter, 0);
}
